//
//  WBNewFeature.swift
//  Weibo
//
//  Created by apple on 2016/12/10.
//  Copyright © 2016年 yanjun. All rights reserved.
//

import UIKit

class WBNewFeature: UIView,UIScrollViewDelegate
{

    @IBOutlet weak var pagControl: UIPageControl!
    @IBOutlet weak var enterButton: UIButton!
    @IBOutlet weak var scrollView: UIScrollView!
    
    class func WBNewFeature() -> WBNewFeature {
        
        let nib = UINib(nibName: "WBNewFeature", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFeature
        
        v.frame = UIScreen.main.bounds
        
        return v
        
    }

    override func awakeFromNib() {
        
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            let imageName = "new_feature_\(i + 1)"
            let iv = UIImageView(image: UIImage(named: imageName))

            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(iv)
            
            
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        
        enterButton.isHidden = true
    }

    
    @IBAction func enterStatus() {
        
        removeFromSuperview()
    }
    
   
}

extension WBNewFeature {
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == scrollView.subviews.count {
            removeFromSuperview()
        }
        
        enterButton.isHidden = (page != scrollView.subviews.count - 1)
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        enterButton.isHidden = true
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pagControl.currentPage = page
        
        
        pagControl.isHidden = (page == scrollView.subviews.count)
    }
    
    
}

